<?php
/**	
*	FILENAME: 		/libraries/logging.php
*	DESCRIPTION:	This library contains functions used to automate the logging within the MyNova Mobile Server Application.
*	AUTHOR:			Casey Burkhardt
*	VERSION:		1.0.0
*	LAST MODIFIED:	12/31/2009
**/

require_once($_SERVER["DOCUMENT_ROOT"] . "/config/environment.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/libraries/functions.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/libraries/admin_privileges.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/libraries/users.php");
require_once($_SERVER["DOCUMENT_ROOT"] . "/libraries/devices.php");

setTimezone();

global $LOGGING_PATH, $TERMINATE_LINE, $CURRENT_TIMESTAMP, $REQUEST_ADDRESS, $FROM_ADDRESS, $UDID;
$LOGGING_PATH = getLoggingPath();
$TERMINATE_LINE = "\r\n";
$CURRENT_TIMESTAMP = date("D M j G:i:s Y");
$REQUEST_ADDRESS = $_SERVER["REMOTE_ADDR"];
$FROM_ADDRESS = "logging@mynovamobile.com";
// Every request to the MyNova Mobile Server Applicaiton should supply a unique device identifier.
// This will be verifed in each of the logging functions.
$UDID = $_POST["UDID"];

/**
*	DESCRIPTION:	Checks whether or not the designated logging directory exists.
**/
function loggingPathExists() {
	global $LOGGING_PATH;
	return file_exists($LOGGING_PATH);
}

/**
*	DESCRIPTION:	Creates the directory used to maintain log files.
**/
function createLoggingPath() {
	global $LOGGING_PATH;
	return mkdir($LOGGING_PATH, 0600);
}

/**
*	DESCRIPTION:	Sends an email to admin users who recieve error notifications in the case that log files cannot be written.
**/
function sendLoggingErrorMessage($logLine) {
	global $FROM_ADDRESS;
	
	mail(getNotificationAddresses("error_notifications"), "MyNova Mobile Logging Error","<html><body><p><h2>MyNova Mobile Logging Error</h2></p><p>The application was unable to write an event to the primary log file.  The action has continued, but has not been recorded in the application's primary log. The log message was:</p>" . $logLine . "<p><code></code></p></body></html>", "MIME-Version: 1.0" . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . "From: " . $FROM_ADDRESS . "\r\n");
}

/**
*	DESCRIPTION:	Writes a line to the MyNova Mobile Server Application primary log file.
**/
function writeToPrimaryLog($line) {
	global $LOGGING_PATH, $TERMINATE_LINE;
	
	if (!loggingPathExists()) {
		createLoggingPath();
	}
	$logFile = @fopen($LOGGING_PATH . "/" . date("Y-m-d") . "-primary.log", "a");
	if ($logFile === FALSE) {
		sendLoggingErrorMessage($line);
	} else {
		if (@fwrite($logFile, $line . $TERMINATE_LINE) === FALSE) {
			sendLoggingErrorMessage($line);
		}
		@fclose($logFile);
	}
}

/**
*	DESCRIPTION:	Writes a line to the MyNova Mobile Server Application error log file.
**/
function writeToErrorLog($line) {
	global $LOGGING_PATH, $TERMINATE_LINE;
	
	if (!loggingPathExists()) {
		createLoggingPath();
	}
	$logFile = @fopen($LOGGING_PATH . "/" . date("Y-m-d") . "-error.log", "a");
	if ($logFile === FALSE) {
		sendLoggingErrorMessage($line);
	} else {
		if (@fwrite($logFile, $line . $TERMINATE_LINE) === FALSE) {
			sendLoggingErrorMessage($line);
		}
		@fclose($logFile);
	}
}

/**
*	DESCRIPTION:	Writes a line to the MyNova Mobile Server Application security log file.
**/
function writeToSecurityLog($line) {
	global $LOGGING_PATH, $TERMINATE_LINE;
	
	if (!loggingPathExists()) {
		createLoggingPath();
	}
	$logFile = @fopen($LOGGING_PATH . "/" . date("Y-m-d") . "-security.log", "a");
	if ($logFile === FALSE) {
		sendLoggingErrorMessage($line);
	} else {
		if (@fwrite($logFile, $line . $TERMINATE_LINE) === FALSE) {
			sendLoggingErrorMessage($line);
		}
		@fclose($logFile);
	}
}

/**
*	DESCRIPTION:	Logs an application security violation and notifies developers according to their notification preferences.
**/
function logSecurityViolation($message) {
	global $LOGGING_PATH, $CURRENT_TIMESTAMP, $REQUEST_ADDRESS, $FROM_ADDRESS, $UDID;
	
	// Create and write the log message.
	$logMessage = "[" . $CURRENT_TIMESTAMP . "] " . "[security violation] [" . $REQUEST_ADDRESS . "] ";
	
	// Translate the UDID to an LDAP Username, if possible.
	$uid = getUIDValue($UDID);
	$ldapUsername = getLDAPUsername($uid);
	if ($uid === FALSE || $ldapUsername === FALSE) {
		$logMessage .= "[No Associated Username, UDID: " . $UDID . "] - ";
	} else {
		$logMessage .= "[" . $ldapUsername . "] - ";
	}
	$logMessage .= $message;
	writeToPrimaryLog($logMessage);
	writeToSecurityLog($logMessage);
	
	// Send Email Notifications
	$to = getNotificationAddresses("security_notifications");
	mail($to, "MyNova Mobile Security Violation","<html><body><p><h2>MyNova Mobile Security Violation</h2></p><p><code>" . $logMessage . "</code></p></body></html>", "MIME-Version: 1.0" . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . "From: " . $FROM_ADDRESS . "\r\n");
}

/**
*	DESCRIPTION:	Logs an application error and notifies developers according to their notification preferences.
**/
function logError($message) {
	global $LOGGING_PATH, $CURRENT_TIMESTAMP, $REQUEST_ADDRESS, $FROM_ADDRESS, $UDID;
	
	// Create and write the log message.
	$logMessage = "[" . $CURRENT_TIMESTAMP . "] " . "[error] [" . $REQUEST_ADDRESS . "] ";
	
	// Translate the UDID to an LDAP Username, if possible.
	$uid = getUIDValue($UDID);
	$ldapUsername = getLDAPUsername($uid);
	if ($uid === FALSE || $ldapUsername === FALSE) {
		$logMessage .= "[No Associated Username, UDID: " . $UDID . "] - ";
	} else {
		$logMessage .= "[" . $ldapUsername . "] - ";
	}
	$logMessage .= $message;
	writeToPrimaryLog($logMessage);
	writeToErrorLog($logMessage);
	
	// Send Email Notifications
	$to = getNotificationAddresses("error_notifications");
	mail($to, "MyNova Mobile Error Notification","<html><body><p><h2>MyNova Mobile Error Notification</h2></p><p><code>" . $logMessage . "</code></p></body></html>", "MIME-Version: 1.0" . "\r\n" . 'Content-type: text/html; charset=iso-8859-1' . "\r\n" . "From: " . $FROM_ADDRESS . "\r\n");
}

/**
*	DESCRIPTION:	Logs an application warning.
**/
function logWarning($message) {
	global $LOGGING_PATH, $CURRENT_TIMESTAMP, $REQUEST_ADDRESS, $FROM_ADDRESS, $UDID;
	
	// Create and write the log message.
	$logMessage = "[" . $CURRENT_TIMESTAMP . "] " . "[warning] [" . $REQUEST_ADDRESS . "] ";
	
	// Translate the UDID to an LDAP Username, if possible.
	$uid = getUIDValue($UDID);
	$ldapUsername = getLDAPUsername($uid);
	if ($uid === FALSE || $ldapUsername === FALSE) {
		$logMessage .= "[No Associated Username, UDID: " . $UDID . "] - ";
	} else {
		$logMessage .= "[" . $ldapUsername . "] - ";
	}
	$logMessage .= $message;
	writeToPrimaryLog($logMessage);
}

/**
*	DESCRIPTION:	Logs an application action.
**/
function logAction($message) {
	global $LOGGING_PATH, $CURRENT_TIMESTAMP, $REQUEST_ADDRESS, $FROM_ADDRESS, $UDID;
	
	// Create and write the log message.
	$logMessage = "[" . $CURRENT_TIMESTAMP . "] " . "[action] [" . $REQUEST_ADDRESS . "] ";
	
	// Translate the UDID to an LDAP Username, if possible.
	$uid = getUIDValue($UDID);
	$ldapUsername = getLDAPUsername($uid);
	if ($uid === FALSE || $ldapUsername === FALSE) {
		$logMessage .= "[No Associated Username, UDID: " . $UDID . "] - ";
	} else {
		$logMessage .= "[" . $ldapUsername . "] - ";
	}
	$logMessage .= $message;
	writeToPrimaryLog($logMessage);
}
?>